<?php

/**
 * This is the model class for table "joke".
 *
 * The followings are the available columns in table 'joke':
 * @property string $id
 * @property string $name
 * @property string $alias
 * @property string $image
 * @property string $desc
 * @property string $content
 * @property string $view
 * @property string $fb_like_count
 * @property string $fb_comment_count
 * @property string $fb_posted
 * @property string $created
 * @property string $reviewed
 * @property string $user_id
 * @property string $status
 * @property string $crawl
 * @property string $crawl_id
 * @property string $crawl_image
 *
 * The followings are the available model relations:
 * @property User $user
 */
class Joke extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Joke the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'joke';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, alias, desc, content, user_id, status', 'required'),
            array('name, alias, image, crawl_id, crawl_image', 'length', 'max'=>255),
			array('desc', 'length', 'max'=>500),
			array('view, fb_like_count, fb_comment_count, user_id', 'length', 'max'=>10),
			array('status', 'length', 'max'=>8),
			array('crawl', 'length', 'max'=>50),
			array('fb_posted, reviewed', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, alias, image, desc, content, view, fb_like_count, fb_comment_count, fb_posted, created, reviewed, user_id, status, crawl, crawl_id, crawl_image', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'alias' => 'Alias',
			'image' => 'Image',
			'desc' => 'Desc',
			'content' => 'Content',
			'view' => 'View',
			'fb_like_count' => 'Fb Like Count',
			'fb_comment_count' => 'Fb Comment Count',
			'fb_posted' => 'Fb Posted',
			'created' => 'Created',
			'reviewed' => 'Reviewed',
			'user_id' => 'User',
			'status' => 'Status',
			'crawl' => 'Crawl',
			'crawl_id' => 'Crawl',
			'crawl_image' => 'Crawl Image',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('view',$this->view,true);
		$criteria->compare('fb_like_count',$this->fb_like_count,true);
		$criteria->compare('fb_comment_count',$this->fb_comment_count,true);
		$criteria->compare('fb_posted',$this->fb_posted,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('reviewed',$this->reviewed,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('crawl',$this->crawl,true);
		$criteria->compare('crawl_id',$this->crawl_id,true);
		$criteria->compare('crawl_image',$this->crawl_image,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    public function getGroupFolder($id = NULL){
        $id = $id ? $id : $this->id;
        for($i = 1000; $i <= 100000000; $i += 1000){
            if($id <= $i) return $i;
        }
    }  
    
        public function getStatusData(){
            return array(
                'APPROVED' => Yii::t('app', 'Approved'),
                'PENDING' => Yii::t('app', 'Pending'),
                'DISABLE' => Yii::t('app', 'Disable'),
            );
        }

        public function getIsApproved(){
            return $this->status == "APPROVED";
        }
        public function getIsPending(){
            return $this->status == "PENDING";
        }
        public function getIsDisable(){
            return $this->status == "DISABLE";
        }

        public function getUrl(){
            return 'http://'.Yii::app()->params->domain.Yii::app()->createUrl('/hehe/web/joke/view',  array('id' => $this->id)); 
        }

        public function getUrlUpdate(){
            $params = array('id' => $this->id);
            return 'http://'.Yii::app()->params->domain.Yii::app()->createUrl('/hehe/web/joke/update', $params); 
        } 
        
        public function getImageUrl(){
            $img_conf = Yii::app()->params->joke_img;
            $url = 'http://'.Yii::app()->params->domain."/{$img_conf['path']}".$this->getGroupFolder()."/{$this->image}.jpg";
            return $url;
        }


        public function getNameEncode(){
            return CHtml::encode($this->name);
        }

        public function getCreatedTime(){
            $created = $this->reviewed ? $this->reviewed : $this->created;
            $format = date('G', strtotime($this->created)) ? 'G:i d/m/Y' : 'd/m/Y';
            return date($format, strtotime($created));
            //return MyDateTime::relative($this->created);
        }
        
        public function getUserName(){
            return $this->user ? $this->user->name : null;
        }
        
        private $_newer;
        public function getNewer(){
            if($this->_newer) return $this->_newer;
            return $this->_newer = $this->find(new CDbCriteria(array(
                'condition' => "t.id > {$this->id} AND t.`status` = 'APPROVED'",
                'order' => 't.id ASC',
            )));
        }

        private $_older;
        public function getOlder(){
            if($this->_older) return $this->_older;
            return $this->_older = $this->find(new CDbCriteria(array(
                'condition' => "t.id < {$this->id} AND t.`status` = 'APPROVED'",
                'order' => 't.id DESC',
            )));
        }

        public function getIsAdminOwner(){
            if(Yii::app()->user->isGuest) return FALSE;
            if(Yii::app()->user->userInfo->isAdmin) return TRUE;
            $image = Image::model()->findByAttributes(array(
                'id' => $this->id,
                'user_id' => Yii::app()->user->id,
            ));
            return $image ? TRUE : FALSE;
        }
        public function getIsModOwner(){
            if(Yii::app()->user->isGuest) return FALSE;
            if(Yii::app()->user->userInfo->isMod) return TRUE;
            $image = Image::model()->findByAttributes(array(
                'id' => $this->id,
                'user_id' => Yii::app()->user->id,
            ));
            return $image ? TRUE : FALSE;
        }
        


    
}